package com.example.smartcitymovie.ui.selfcenter;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;
import androidx.room.Room;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.smartcitymovie.MyApplication;
import com.example.smartcitymovie.R;
import com.example.smartcitymovie.bean.LoginBean;
import com.example.smartcitymovie.bean.ResultBean;
import com.example.smartcitymovie.dao.AppDatabase;
import com.example.smartcitymovie.dao.Userdao;
import com.example.smartcitymovie.databinding.FragmentLoginBinding;
import com.example.smartcitymovie.entity.User;
import com.example.smartcitymovie.utils.OkHttpUtils;

import java.io.IOException;
import java.util.Objects;

import cn.hutool.crypto.digest.MD5;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;


public class Login extends Fragment {

    FragmentLoginBinding binding;
    Userdao userdao;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentLoginBinding.inflate(inflater);
        initView();
        AppDatabase db = Room.databaseBuilder(requireActivity(),
                AppDatabase.class, "database").build();
        userdao = db.userDao();
        return binding.getRoot();
    }

    private void initView() {
        binding.btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LoginBean loginBean = new LoginBean();
                loginBean.setUsername(binding.TIETUserName.getText().toString());
                loginBean.setPassword(binding.TIETPassword.getText().toString());
                if (loginBean.getPassword()==null|| loginBean.getPassword().isEmpty()){
                    Toast.makeText(getActivity(), "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (loginBean.getUsername()==null|| loginBean.getUsername().isEmpty()){
                    Toast.makeText(getActivity(), "请输入用户名", Toast.LENGTH_SHORT).show();
                    return;
                }
                new Thread(() -> {
                    MD5 md5 = MD5.create();

                    User user = userdao.selectUser(loginBean.getUsername(),  md5.digestHex(loginBean.getPassword()));
                    if (user!=null){
                        MyApplication.getSharedPreferencesEditor()
                                .putBoolean("isLogin",true)
                                .putInt("userId",user.id)
                                .apply();
                       requireActivity().runOnUiThread(() -> {
                           Toast.makeText(getActivity(), "登录成功，正在前往个人中心", Toast.LENGTH_SHORT).show();
                           Navigation.findNavController(requireActivity(), R.id.nav_host_fragment_activity_main).navigate(R.id.action_navigation_Login_to_navigation_selfCenter);
                       });
                    }else {
                       requireActivity().runOnUiThread(()->{
                           Toast.makeText(getActivity(), "用户名密码错误", Toast.LENGTH_SHORT).show();
                       });
                    }
                }).start();

            }
        });


        binding.btnReg.setOnClickListener(v -> {
            Navigation.findNavController(requireActivity(), R.id.nav_host_fragment_activity_main).navigate(R.id.action_navigation_Login_to_registerFragment);
        });
    }
}